﻿using System;
using System.Windows.Input;
using AzureTracer.Common;
using AzureTracer.Model;
using AzureTracer.Resources;
using AzureTracer.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;

namespace AzureTracer.ViewModels
{
    public class LoginViewModel : ViewModelBase
    {
        public const string WARNINGDIALOG_MESSAGE = "ShowWarningDialog";
        private ILogService _logService;
        private IAppSettings _appSettings;
        private INavigationService _navigationService;

        public ILogService LogService { get { return _logService; } }
        public ProgressIndicatorViewModel StateInfo { get; private set; }
        public ICommand LoginCommand { get; private set; }


        public LoginViewModel()
            :this(ViewModelLocator.AppSettings,ViewModelLocator.LogService,ViewModelLocator.NavigationService) {}

        [PreferredConstructor]
        public LoginViewModel(IAppSettings appSettings, ILogService logService, INavigationService navigationService)
        {
            _appSettings = appSettings;
            _logService = logService;
            _navigationService = navigationService;

            StateInfo = new ProgressIndicatorViewModel();
#if(DEBUG)
             _accountName = "xxxx";
             _accountKey = "xxxxxxxx";        
#endif
            LoginCommand = new RelayCommand(OnLogin);
        }

        #region AccountName NotifyProperty
        private string _accountName;
        public string AccountName
        {
            get { return _accountName; }
            set
            {
                _accountName = value;
                RaisePropertyChanged("AccountName");
            }
        } 
        #endregion

        #region AccountKey NotifyProperty
        private string _accountKey;
        public string AccountKey
        {
            get { return _accountKey; }
            set
            {
                _accountKey = value;
                RaisePropertyChanged("AccountKey");
            }
        }
        #endregion   

        #region Commands Events
        private void OnLogin()
        {
            if (string.IsNullOrWhiteSpace(AccountName)
                || string.IsNullOrWhiteSpace(AccountKey))
            {
                Messenger.Default.Send<string>
                    (labels.RequiredAccount, WARNINGDIALOG_MESSAGE);
                return;
            }
            if (AccountKey.Length != 88)
            {
                Messenger.Default.Send<string>
                     (labels.BadLeghtKey, WARNINGDIALOG_MESSAGE);
                return;
            }

            StateInfo.StartLoading(labels.CheckingLogin);

            try
            {
                _logService.CheckService(AccountName, AccountKey, (ex) =>
                    {
                        StateInfo.EndLoading();
                        if (ex == null)
                        {
                            _appSettings.AccountName = AccountName;
                            _appSettings.AccountKey = AccountKey;
                            AccountName = AccountKey = string.Empty;
                            _logService.LogList.Clear();
                            _navigationService.Back();
                        }
                        else
                        {
                            _appSettings.AccountName = _appSettings.AccountKey = string.Empty;
                            Messenger.Default.Send<string>(ResourceHelper.ExceptionMessage(ex), WARNINGDIALOG_MESSAGE);
                        }
                    });
            }
            catch (FormatException)
            {
                StateInfo.EndLoading();
                Messenger.Default.Send<string>(labels.BadKey, WARNINGDIALOG_MESSAGE);
            }            
        }
        #endregion
    }
}
